package com.homedo.microservice.open.service.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.bean.po.RouteServiceUrl;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.RouteServiceUrlEnhanced;
import com.homedo.microservice.open.service.gateway.persistence.dao.RouteServiceUrlDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月18日 11时30分20秒
 */
@Service
public class RouteServiceUrlService extends BaseService<RouteServiceUrl> {
    @Autowired
    private RouteServiceUrlDao routeServiceUrlDao;


    @Override
    public List<RouteServiceUrlEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<RouteServiceUrl> routeServiceUrls = routeServiceUrlDao.getListByIds(ids, RouteServiceUrl.class);

        if (!Detect.notEmpty(routeServiceUrls)) {
            return null;
        }

        return this.convent2Enhanceds(routeServiceUrls);
    }


    @Override
    public List<RouteServiceUrlEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<RouteServiceUrl> routeServiceUrls = routeServiceUrlDao.getListByIdsGreaterThanMark0(ids, RouteServiceUrl.class);

        if (!Detect.notEmpty(routeServiceUrls)) {
            return null;
        }

        return this.convent2Enhanceds(routeServiceUrls);
    }


    @Override
    public List<RouteServiceUrlEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<RouteServiceUrl> routeServiceUrls = (List<RouteServiceUrl>) pos;
        List<RouteServiceUrlEnhanced> routeServiceUrlEnhanceds = Lists.newArrayList();
        routeServiceUrls.forEach(e -> routeServiceUrlEnhanceds.add(new RouteServiceUrlEnhanced(e)));
        return routeServiceUrlEnhanceds;
    }


    public List<RouteServiceUrlEnhanced> getEnhanceds4Services(List<String> serviceIds, UserContext userContext) {
        List<RouteServiceUrl> routeServiceUrls = routeServiceUrlDao.getListByServiceIds(serviceIds);

        if (!Detect.notEmpty(routeServiceUrls)) {
            return null;
        }

        return this.convent2Enhanceds(routeServiceUrls);
    }
}
